/*
 * Copyright 2009 Bushel Project Members (http://bushel.googlecode.com/)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.bushel.ant;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.jar.Manifest;

import org.apache.ivy.ant.IvyTask;
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter;
import org.apache.tools.ant.BuildException;

import com.googlecode.bushel.core.BundleInfo;
import com.googlecode.bushel.core.ManifestParser;
import com.googlecode.bushel.repo.BundleInfoAdapter;
import com.googlecode.bushel.repo.osgi.ExecutionEnvironmentProfileProvider;

public class ConvertManifestTask extends IvyTask {

    private File manifest = null;

    private File ivyFile = null;

    private ExecutionEnvironmentProfileProvider profileProvider;

    public void setProfileProvider(ExecutionEnvironmentProfileProvider profileProvider) {
        this.profileProvider = profileProvider;
    }

    public void setManifest(File manifest) {
        this.manifest = manifest;
    }

    public void setIvyFile(File ivyFile) {
        this.ivyFile = ivyFile;
    }

    @Override
    public void doExecute() throws BuildException {
        if (ivyFile == null) {
            throw new BuildException("destination ivy file is required for convertmanifest task");
        }
        if (manifest == null) {
            throw new BuildException("source manifest file is required for convertmanifest task");
        }

        Manifest m;
        try {
            m = new Manifest(new FileInputStream(manifest));
        } catch (FileNotFoundException e) {
            throw new BuildException("the manifest file '" + manifest + "' was not found", e);
        } catch (IOException e) {
            throw new BuildException("the manifest file '" + manifest + "' could not be read", e);
        }

        BundleInfo bundleInfo;
        try {
            bundleInfo = ManifestParser.parseManifest(m);
        } catch (ParseException e) {
            throw new BuildException("Incorrect manifest file '" + manifest + "'", e);
        }
        ModuleDescriptor md = BundleInfoAdapter.toModuleDescriptor(bundleInfo, profileProvider);

        try {
            XmlModuleDescriptorWriter.write(md, ivyFile);
        } catch (IOException e) {
            throw new BuildException("The ivyFile '" + ivyFile + "' could not be written", e);
        }
    }

}
